
public class Cursor {
int x=0,y=0,mx,my,dx,dy;
Rally rally;
Tileable[][][] tilables;
int[][] sizes;
int[] select={0,0,0,0};


public Cursor(int x, int y, int mx, int my, Tileable[][][] tilables, int[][] sizes)
{
this.x=x;
this.y=y;
this.mx=mx;
this.my=my;
this.dx=-1;
this.dy=-1;
this.tilables=tilables;
this.sizes=sizes;
}

public Tileable getSelected()
{
	if(tilables[this.select[0]][this.select[1]]==null)
	{
		return null;	
	}
	else if(tilables[this.select[0]][this.select[1]][this.select[2]]==null)
	{
		return null;	
	}
	else
	{
		return 	tilables[this.select[0]][this.select[1]][this.select[2]];
	}
}

public Tileable[][] getUnits()
{
	//Return all units
	return tilables[0];
}
public Tileable[][] getBuildings()
{
	//Return all buildings
	return tilables[1];
}

public int getOrder()
{
	//Return current command
	return this.select[3];
}

public int getdx()
{
	return this.dx;
}
public int getdy()
{
	return this.dy;
}

public void setdxdy(int dx, int dy)
{
	this.dx=dx;
	this.dy=dy;
}

public void incdxdy(int dx, int dy)
{
	this.dx+=dx;
	this.dy+=dy;
}

public void toggle(int mode, int value)
{
	//Toggle between selection mode/types/elements/commands
	int size=0;
	if(this.rally!=null)
	{
	sizes[3][0]=this.rally.getSize()[0];
	sizes[3][1]=this.rally.getSize()[1];
	sizes[3][2]=this.rally.getSize()[2];
	}
	else
	{
		sizes[3][0]=0;
		sizes[3][1]=0;
		sizes[3][2]=0;	
	}
	
	
		if (mode==0) //units/buildings
		{
			this.select[mode]=select[mode]+value;
			size=tilables.length-1;
			this.select[1]=0;
			this.select[2]=0;
			this.select[3]=0;
			this.dx=-1;
			this.dy=-1;
		}
		else if(mode==1) //types
		{
			size=tilables[select[0]].length;
			for(int i=1;i<size;i++)
			{
			int n=(this.select[mode]+i*value)%size;
			if (n<0){n=size+n;}
			if(sizes[select[0]][n]>0)
			{
				this.select[mode]=n;
				break;
			}
			}			
			
			this.select[2]=0;
			this.select[3]=0;
			this.dx=-1;
			this.dy=-1;
		}
		else if(mode==2) //elements
		{
			this.select[mode]=select[mode]+value;
			size=sizes[select[0]][select[1]]-1;
				
			this.select[3]=0;
			this.dx=-1;
			this.dy=-1;
		}
		else if(mode==3) //commands
		{
			this.select[mode]=select[mode]+value;
			try{
			size = tilables[select[0]][select[1]][select[2]].gettxtCommands().length-1;
			}catch(Exception e){}
			this.dx=-1;
			this.dy=-1;
		}
		

		//Make sure selected element is between [0, size)
		if(size<0)
		{
			this.select[mode]=0;
		}
		else
		{
		if(this.select[mode]<0)
		{
			this.select[mode]=size;
		}
		else if (select[mode]>size)
		{
			this.select[mode]=0;
		}
		
		if(this.select[0]==2)
		{
			this.rally=((Rally)tilables[2][this.select[1]][this.select[2]]);
			tilables[3][0]=this.rally.allUnits;
			tilables[3][1]=this.rally.battleUnits;
			tilables[3][2]=this.rally.supportUnits;
			sizes[3][0]=this.rally.getSize()[0];
			sizes[3][1]=this.rally.getSize()[1];
			sizes[3][2]=this.rally.getSize()[2];
		}
		
		}
		
		//Update current cursor position to selected element
		try
		{
		this.x=tilables[this.select[0]][this.select[1]][this.select[2]].getX();
		this.y=tilables[this.select[0]][this.select[1]][this.select[2]].getY();
		}
		catch (Exception e) {}
}

public void goTo(int x)
{
	if(tilables[this.select[0]][this.select[1]][x]!=null)
	{
		select[2]=x;
		this.dx=-1;
		this.dy=-1;
	}		
	
	//Update current cursor position to selected element
	this.x=tilables[this.select[0]][this.select[1]][this.select[2]].getX();
	this.y=tilables[this.select[0]][this.select[1]][this.select[2]].getY();
}

public String gettxtMode()
{
	switch(this.select[0])
	{
	case 0: return "Units";
	case 1: return "Buildings";
	case 2: return "Rally";
	case 3: 
		switch(this.select[1])
		{
		case 0: return "Entire Army";
		case 1: return "Battle Army";
		case 2: return "Support Army";
		default:  return "Armies";
		}
		
	default: return "";
	}
}

public int getSize(int mode)
{
return sizes[mode][0];
}

public int getx()
{
	if((tilables[this.select[0]] == null) || (tilables[this.select[0]][this.select[1]] == null) || (tilables[this.select[0]][select[1]][select[2]]==null))
	{
	return x;
	}
	else
	{
	return tilables[this.select[0]][this.select[1]][this.select[2]].getX();
	}
}
public int gety()
{
	if((tilables[this.select[0]] == null) || (tilables[this.select[0]][this.select[1]] == null) || (tilables[this.select[0]][this.select[1]][this.select[2]]==null))
	{
	return y;
	}
	else
	{
	return tilables[this.select[0]][this.select[1]][this.select[2]].getY();
	}
}
public int getmx()
{
	return this.mx;	
}
public int getmy()
{
	return this.my;
}
}
